package com.naiterui.ehp.bs.recommend.controller.inner;

import springfox.documentation.annotations.ApiIgnore;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.bo.recommend.param.RebuyNoticeParamBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IRebuyNoticeService;

/**
 * 复购提醒内部接口Controller
 *
 * @since 2.19
 */
@RequestMapping("recommend/inner/rebuy/notice")
@Controller
@ApiIgnore
public class InnerRebuyNoticeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InnerRebuyNoticeController.class);

    @Autowired
    IRebuyNoticeService rebuyNoticeService;

    /**
     * 生成复购提醒记录
     * InnerRebuyNoticeController.creat()
     *
     * @param paramVO
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年6月27日
     * @since 2.19
     */

    @RequestMapping("create")
    @ResponseBody
    public ResponseVO<Void> create(@RequestBody RebuyNoticeParamBO paramVO) throws BusinessException {
        LOGGER.info("下单回调，开始生产复购提醒记录...paramVO:{}", paramVO);
        if (paramVO == null || paramVO.getPatientId() == null || paramVO.getBaseOrderId() == null
                || CollectionUtils.isEmpty(paramVO.getRecomBuyList())) {
            LOGGER.error("调用生成提醒消息内部接口参数错误, paramVO: {}", paramVO);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Long patientId = paramVO.getPatientId();
        Long baseOrderId = paramVO.getBaseOrderId();
        rebuyNoticeService.saveRebuyNotice(patientId, baseOrderId, paramVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 删除未发送的复购提醒信息
     * InnerRebuyNoticeController.removeUnsend()
     *
     * @param patientId
     * @param visitTime 8位年月日
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年6月29日
     * @since 2.19
     */

    @RequestMapping("removeUnsend")
    @ResponseBody
    public ResponseVO<Void> removeUnsend(Long patientId, String visitTime) throws BusinessException {
        LOGGER.info("删除未发送复购提醒信息, patientId: {},time:{}", patientId, visitTime);
        if (patientId == null) {
            LOGGER.error("删除未发送复购提醒信息内部接口参数错误, patientId: {}", patientId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        rebuyNoticeService.removeUnsendNotice(patientId, visitTime);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 删除指定订单的复购提醒
     * InnerRebuyNoticeController.removeByOrderId()
     *
     * @param baseOrderId
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年6月30日
     * @since 2.19
     */

    @RequestMapping("removeByOrderId")
    @ResponseBody
    public ResponseVO<Void> removeByOrderId(Long baseOrderId) throws BusinessException {
        if (baseOrderId == null) {
            LOGGER.error("删除未发送复购提醒信息内部接口参数错误, baseOrderId: {}", baseOrderId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        rebuyNoticeService.removeByOrderId(baseOrderId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
